package com.zjy.order.controller;

import com.zjy.basic.domain.dto.OrderAdoptDto;
import com.zjy.order.service.OrderAdoptService;
import com.zjy.order.domain.OrderAdopt;
import com.zjy.order.query.OrderAdoptQuery;
import com.zjy.basic.util.AjaxResult;
import com.zjy.basic.util.PageList;
import com.zjy.pet.domain.Pet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/orderAdopt")
public class OrderAdoptController {

    @Autowired
    public OrderAdoptService orderAdoptService;

    /**
     * 接口：添加或修改
     * @param orderAdopt  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult save(@RequestBody OrderAdopt orderAdopt){
        try {
            if( orderAdopt.getId()!=null)
                orderAdoptService.update(orderAdopt);
            else
                orderAdoptService.add(orderAdopt);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            orderAdoptService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
        e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[] ids){
        try {
                orderAdoptService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public OrderAdopt findById(@PathVariable("id")Long id) {
        return orderAdoptService.findById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<OrderAdopt> findAll(){
        return orderAdoptService.findAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<OrderAdopt> queryPage(@RequestBody OrderAdoptQuery query) {
        return orderAdoptService.queryPage(query);
    }

    @PostMapping("/submitOrder")
    public AjaxResult submitOrder(@RequestBody Pet pet, HttpServletRequest request) throws Exception {
        return orderAdoptService.submitOrder(pet,request);
    }
}
